package com.opal.jx.service.impl;

import com.github.pagehelper.PageInfo;
import com.opal.consts.WSC;
import com.opal.domain.R;
import com.opal.jx.JxConst.JxConst;
import com.opal.jx.entity.table.*;
import com.opal.jx.mapper.mysql.JxEmpMapper;
import com.opal.jx.service.JxAssessCommonService;
import com.opal.jx.service.JxEmpService;
import com.opal.jx.service.JxPostService;
import com.opal.model.DataGridData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class JxEmpServiceImpl implements JxEmpService {
    @Autowired
    private JxEmpMapper jxEmpMapper;
    @Autowired
    private JxPostService jxPostService;
    @Autowired
    private JxAssessCommonService jxAssessCommonService;

    @Override
    public List<JxEmp> selectJxEmp() {
        JxEmpExample jxEmpExample= new JxEmpExample();
        List<JxEmp> jxEmps = jxEmpMapper.selectByExample(jxEmpExample);
        return jxEmps;
    }

    @Override
    public JxEmp selectJxEmpById(Long id) {
        JxEmp jxEmp = jxEmpMapper.selectByPrimaryKey(id);
        return jxEmp;
    }

    @Override
    public List<JxEmp> selectJxEmpByCondition(JxEmp condition) {
        JxEmpExample jxEmpExample = this.getJxEmpExample(condition);
        return jxEmpMapper.selectByExample(jxEmpExample);
    }

    @Override
    public int addJxEmp(JxEmp jxEmp) {
        int i = jxEmpMapper.insertSelective(jxEmp);
        return i;
    }

    @Override
    public int modifyJxEmpById(JxEmp jxEmp,Long ...id) {
        if (id !=null){
                jxEmp.setId(id[0]);
                return jxEmpMapper.updateByPrimaryKey(jxEmp);
        }else {
            //若没有值,则主键id已经在对象中
            return jxEmpMapper.updateByPrimaryKey(jxEmp);
        }

    }

    @Override
    public int modifyJxEmpByCondition(JxEmp jxEmp, JxEmp condition) {
        if (condition !=null || jxEmp !=null){
            int i = jxEmpMapper.updateByExample(jxEmp, this.getJxEmpExample(condition));
            return i;
        }

        return 0;
    }

    @Override
    public List<JxEmp> selectJxEmpByCondition(String pkDept, List<Long> postIds) {
        JxEmpExample jxEmpExample=new JxEmpExample();
        jxEmpExample.createCriteria().andPkDeptEqualTo(pkDept).andPostIdIn(postIds);
        List<JxEmp> jxEmps = jxEmpMapper.selectByExample(jxEmpExample);
        return jxEmps;
    }

    @Override
    public List<JxEmp> selectJxEmpByPkDeptAndPostIds(String pkDept, List<Long> postIds) {
        //根据当前部门\多个岗位名称查询主管、和组长人员信息
        JxEmpExample jxEmpExample = new JxEmpExample();
        jxEmpExample.createCriteria().andPkDeptEqualTo(pkDept).andPostIdIn(postIds);
        return jxEmpMapper.selectByExample(jxEmpExample);
    }

    @Override
    public List<JxEmp> selectJxEmpByPkDeptAndPostIds(String pkDept, String empZhuGuan, String postId){
        //根据当前部门\多个岗位名称查询主管、和组长人员信息
        JxEmpExample jxEmpExample = new JxEmpExample();
        JxEmpExample.Criteria criteria = jxEmpExample.createCriteria();

        criteria.andPostIdEqualTo(Long.parseLong(postId));
        if (!StringUtils.isBlank(pkDept)){
            criteria.andPkDeptEqualTo(pkDept);
        }
        if (!StringUtils.isBlank(empZhuGuan)){
            criteria.andEmpZhuguanEqualTo(empZhuGuan);
        }

        return jxEmpMapper.selectByExample(jxEmpExample);
    }

    @Override
    public List<JxEmp> selectJxEmpByCondition(String pkDept, List<String> empZhuguans, Long postId) {
        JxEmpExample jxEmpExample = new JxEmpExample();
        jxEmpExample.createCriteria().andPkDeptEqualTo(pkDept).andEmpZhuguanIn(empZhuguans).andPostIdEqualTo(postId);

        return jxEmpMapper.selectByExample(jxEmpExample);
    }

    @Override
    public List<JxEmp> selectJxEmpByPkDeptAndPostNames(String pkDept, List<String> postNames) {
        JxEmpExample jxEmpExample = new JxEmpExample();
        jxEmpExample.createCriteria().andPkDeptEqualTo(pkDept).andPostNameIn(postNames);
        return jxEmpMapper.selectByExample(jxEmpExample);
    }

    @Override
    public Object getJxEmpPostService(String pkDept, String pkOrg) {
        JxPost jxPost= new JxPost();
        jxPost.setPkDept(pkDept);
        jxPost.setPkOrg(pkOrg);
        List<JxPost> jxPosts = jxPostService.getJxPostBycondition(jxPost);
        return jxPosts;
    }

    @Override
    public Object getjxPostAll() {
        JxPost jxPost= new JxPost();
        List<JxPost> jxPosts = jxPostService.getJxPostBycondition(jxPost);
        return jxPosts;
    }

    @Override
    public R saveJxEmp(JxEmp jxEmp){
        jxEmp= deptNameAndPostName(jxEmp);
        int i = jxEmpMapper.insertSelective(jxEmp);
        if (i>0){
            return new R(WSC.RESTPONSE_OK,JxConst.SAVE_SUCCESS);
        }else {
            return new R(WSC.PRECONDITIONFAILED,JxConst.SAVE_DEFEATED);
        }
    }

    @Override
    public R updateJxEmp(JxEmp jxEmp) {
        jxEmp= deptNameAndPostName(jxEmp);
        if (jxEmp.getId()==null){
            return new R(WSC.PRECONDITIONFAILED,JxConst.UPDATE_EFEATED2);
        }
        int i = jxEmpMapper.updateByPrimaryKey(jxEmp);
        if (i>0){
            return new R(WSC.RESTPONSE_OK,JxConst.UPDATE_SUCCESS);
        }else {
            return new R(WSC.PRECONDITIONFAILED,JxConst.UPDATE_EFEATED);
        }
    }

    @Override
    public  DataGridData<List<JxEmp>> selectJxEmpUseEayuiTableService(int page,int rows,JxEmp jxEmp) {
        com.github.pagehelper.PageHelper.startPage(page, rows);
        List<JxEmp> jxEmps = this.queryJxempDataRefreshTable(jxEmp);
        PageInfo<JxEmp> pageInfo= new PageInfo<>(jxEmps);
        long total = pageInfo.getTotal();
        //封装数据
        DataGridData<List<JxEmp>> dataGridData= new DataGridData<>();
        dataGridData.setRows(jxEmps);
        dataGridData.setTotal((int)total);
        return dataGridData;
    }

    @Override
    public List<JxEmp>  queryJxempDataRefreshTable(JxEmp jxEmp) {
        JxEmpExample jxEmpExample= new JxEmpExample();
        JxEmpExample.Criteria criteria = jxEmpExample.createCriteria();

        String empName = jxEmp.getEmpName();
        if (!StringUtils.isBlank(empName)){
            criteria.andEmpNameLike("%"+empName+"%");
        }
        String empCode = jxEmp.getEmpCode();
        if (!StringUtils.isBlank(empCode)){
            criteria.andEmpCodeLike("%"+empCode+"%");
        }
        Long postId = jxEmp.getPostId();
        if (postId !=null){
            criteria.andPostIdEqualTo(postId);
        }
        String empGroup = jxEmp.getEmpGroup();
        if (!StringUtils.isBlank(empGroup)){
            criteria.andEmpGroupEqualTo(empGroup);
        }
        String pkOrg = jxEmp.getPkOrg();
        if (!StringUtils.isBlank(pkOrg)){
            criteria.andPkOrgEqualTo(pkOrg);
        }
        String pkDept = jxEmp.getPkDept();
        if (!StringUtils.isBlank(pkDept)){
            criteria.andPkDeptEqualTo(pkDept);
        }

        return jxEmpMapper.selectByExample(jxEmpExample);
    }

    @Override
    public R deleteJxEmp(JxEmp jxEmp) {
        int i = jxEmpMapper.deleteByPrimaryKey(jxEmp.getId());
        if (i>0){
            return new R(WSC.RESTPONSE_OK,JxConst.DELETE_SUCCESS);
        }else {
            return  new R(WSC.PRECONDITIONFAILED,JxConst.DELETE_EFEATED);
        }
    }

    @Override
    public List<JxEmp> selectEmpByLikePostName(String postName,String pkDept) {
        return jxAssessCommonService.selectEmpByLikePostName(postName,pkDept);
    }

    @Override
    public List<JxEmp> selectJxEmpByEmpZhuGuan(String empCode,Long postId) {
        JxEmpExample jxEmpExample = new JxEmpExample();
        jxEmpExample.createCriteria().andEmpZhuguanEqualTo(empCode).andPostIdEqualTo(postId);
        List<JxEmp> jxEmps = jxEmpMapper.selectByExample(jxEmpExample);
        return jxEmps;
    }

    @Override
    public List<JxEmp> selectJxEmpByEmpZuZhang(String empCode,Long postId) {
        JxEmpExample jxEmpExample = new JxEmpExample();
        jxEmpExample.createCriteria().andEmpZuzhangEqualTo(empCode).andPostIdEqualTo(postId);
        List<JxEmp> jxEmps = jxEmpMapper.selectByExample(jxEmpExample);
        return jxEmps;
    }

    @Override
    public List<JxDept> getAllJxDeptService() {
        return jxAssessCommonService.getAllJxDeptService();
    }

    @Override
    public List<JxDept> getJxDeptByPkOrgService(String pkOrg) {
        return jxAssessCommonService.getJxDeptByPkOrg(pkOrg);
    }

    @Override
    public List<JxEmp> getEmpByPostIdService(Long postId) {
        JxEmpExample jxEmpExample = new JxEmpExample();
        jxEmpExample.createCriteria().andPostIdEqualTo(postId);
        List<JxEmp> jxEmps = jxEmpMapper.selectByExample(jxEmpExample);
        return jxEmps;
    }

    @Override
    public List<JxAssessBzcjZg> getZhuGuanInfoService(Long postId){
        List<JxEmp> jxEmps = this.getEmpByPostIdService(postId);
        List<JxAssessBzcjZg> JxAssessBzcjZgs=new ArrayList<>();
        jxEmps.forEach(j->{
            JxAssessBzcjZg jxAssessBzcjZg = new JxAssessBzcjZg();
            jxAssessBzcjZg.setEmpName(j.getEmpName());
            jxAssessBzcjZg.setEmpCode(j.getEmpCode());
            jxAssessBzcjZg.setPostName(j.getPostName());
            JxAssessBzcjZgs.add(jxAssessBzcjZg);
        });
        return JxAssessBzcjZgs;
    }

    private JxEmpExample getJxEmpExample(JxEmp condition){
        JxEmpExample jxEmpExample = new JxEmpExample();
        if (condition !=null){
            JxEmpExample.Criteria criteria = jxEmpExample.createCriteria();
            Integer age = condition.getEmpAge();            //年龄
            String code = condition.getEmpCode();           //工号
            String postName = condition.getPostName();      //岗位名称
            Long postCode = condition.getPostId();       //岗位
            Date entryDate = condition.getEmpEntryDate();   //入职时间
            String gender = condition.getEmpGender();       //性别
            String name = condition.getEmpName();           //姓名
            String pkOrg = condition.getPkOrg();            //组织
            String pkDept = condition.getPkDept();          //部门
            Integer empEntry = condition.getEmpEntry();     //入职月份
            String empGroup = condition.getEmpGroup();      //班组
            String empZuzhang = condition.getEmpZuzhang();      //组长
            String empZhuguan = condition.getEmpZhuguan();      //主管
            String empKezhang = condition.getEmpKezhang();      //科长

            if (empZuzhang !=null){
                criteria.andEmpZuzhangEqualTo(empZuzhang);
            }
            if (empZhuguan !=null){
                criteria.andEmpZhuguanEqualTo(empZhuguan);
            }
            if (empKezhang !=null){
                criteria.andEmpKezhangEqualTo(empKezhang);
            }
            if (empGroup !=null){
                criteria.andEmpGroupEqualTo(empGroup);
            }
            if (age !=null){
                criteria.andEmpAgeEqualTo(age);
            }
            if (!StringUtils.isBlank(code)){
                criteria.andEmpCodeEqualTo(code);
            }
            if (entryDate !=null){
                criteria.andEmpEntryDateEqualTo(entryDate);
            }
            if (!StringUtils.isBlank(gender)){
                criteria.andEmpGenderEqualTo(gender);
            }
            if (!StringUtils.isBlank(name)){
                criteria.andEmpNameEqualTo(name);
            }
            if (!StringUtils.isBlank(pkOrg)){
                criteria.andPkOrgEqualTo(pkOrg);
            }
            if (pkDept !=null){
                criteria.andPkDeptEqualTo(pkDept);
            }
            if (empEntry != null){
                criteria.andEmpEntryEqualTo(empEntry);
            }
            if (postCode != null){
                criteria.andPostIdEqualTo(postCode);
            }
            if (!StringUtils.isBlank(postName)){
                criteria.andPostNameEqualTo(postName);
            }
        }
        return jxEmpExample;
    }

    private  JxEmp deptNameAndPostName(JxEmp jxEmp){
        Long postId = jxEmp.getPostId();
        JxPost post = jxPostService.getJxPostById(postId);
        jxEmp.setPostName(post.getName());
        jxEmp.setPkDeptName(post.getPkDeptName());
        return jxEmp;
    }

}
